-- ship_level_up
-- create by cheny3
-- 幽灵船学习/升级

-- 主动技
local skills = {6201, 6211, 6221, 6231};

return {
    -- 判断
    canTrigger = function(args)
        if not ME.user.ship then
            return false;
        end

        local dungeonId = DungeonM.getDungeonId();
        local layer = DungeonM.currentLayer();
        if DungeonBossM.isBossLayer(dungeonId, layer) then
            -- BOSS层不允许操作
            return false;
        end

        -- 间隔层数
        local step = args["step"] or 1;
        local layer = DungeonM.currentLayer();
        if layer % step ~= 0 then
            return false;
        end

        local shipSkills = ME.user.ship:getSkills();

        -- 判断能否学习/升级主动技
        for _, skillId in pairs(skills) do
            if GhostShipM.canLearnSkill(skillId) == true then
                return true;
            end
        end
        for _, skillId in pairs(shipSkills) do
            if GhostShipM.canUpgradeSkill(skillId) == true then
                return true;
            end
        end

        -- 判断能否能级
        if GhostShipM.canLevelUp() == true then
            return true;
        end

        return false;
    end,

    -- 触发
    trigger = function(args)
        if not ME.user.ship then
            return;
        end

        local shipSkills = ME.user.ship:getSkills();

        -- 判断能否学习/升级主动技
        for _, skillId in pairs(skills) do
            if GhostShipM.canLearnSkill(skillId) == true then
                return DungeonActionM.go("ship_learn_skill", 0, skillId);
            end
        end
        for _, skillId in pairs(shipSkills) do
            if GhostShipM.canUpgradeSkill(skillId) == true then
                return DungeonActionM.go("upgrade_ship_skill", skillId);
            end
        end

        -- 尝试升级
        if GhostShipM.canLevelUp() == true then
            return GhostShipM.levelUp();
        end

        return false;
    end,
};
